package com.lkqandzzy.controller;


import com.lkqandzzy.entity.PageInfo;
import com.lkqandzzy.entity.Record;

import com.lkqandzzy.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class RecordController {
    // 依赖注入
    @Autowired
    private RecordService recordService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findRecord")
    public String findRecord(String customer_name,String theme,
                                    Integer pageIndex, Integer pageSize, Model model) {

        PageInfo<Record> ri = recordService.findPageInfo(customer_name,theme,
                pageIndex,pageSize);
        model.addAttribute("ri",ri);
        return "record_list";
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value = "/exportRecord", method = RequestMethod.POST)
    @ResponseBody
    public List<Record> exportLeavingSchool(){
        List<Record> RecordList = recordService.getAll();
        return RecordList;
    }
    /**
     * 添加跟踪记录信息
     */
    @RequestMapping(value = "/addRecord" ,method = RequestMethod.POST)
    @ResponseBody
    public String addRecord( @RequestBody Record record) {
        int d = recordService.addRecord(record);
        return "record_list";
    }
    /**
     * 删除跟踪记录信息
     */
    @RequestMapping( "/deleteRecord")
    @ResponseBody
    public String deleteRecord(Integer r_id) {
        int d = recordService.deleteRecord(r_id);
        return "record_list";
    }
    /**
     * 修改跟踪记录信息
     */
    @RequestMapping( "/updateRecord")
    public String updateRecord( Record record) {
        int ci = recordService.updateRecord(record);
        return "redirect:/findRecord";
    }
    @RequestMapping( "/findRecordById")
    public String findRecord(Integer r_id, HttpSession session) {

        Record ci= recordService.findRecordById(r_id);
        session.setAttribute("ci",ci);
        return "record_edit";
    }
}
